package cc.orange.mainView;

import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import cc.orange.adapter.MessageFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

import cc.orange.R;
import cc.orange.databinding.FragmentMessageBinding;
import cc.orange.entity.HightEntity;
//import com.tendcloud.tenddata.TalkingDataSDK;

public class MessageFragment extends Fragment {

    private FragmentMessageBinding binding;

    private MessageFragmentAdapter messageFragmentAdapter;

    private List<Fragment> fragments=new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_message,container,false);
        initVP();
        initTab(0);

        EventBus.getDefault().register(this);
        return binding.getRoot();
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getHight(HightEntity entity) {//改变布局
        if(entity.getHight() > 0){
            LinearLayout.LayoutParams pp = new LinearLayout.
                    LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    entity.getHight());
            binding.msgTitle.setLayoutParams(pp);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initVP() {
        fragments.add(new MsgFragment());
        fragments.add(new ChatFriendFragment());
        messageFragmentAdapter = new MessageFragmentAdapter(getActivity(), fragments);
        binding.vpMessage.setAdapter(messageFragmentAdapter);
        binding.vpMessage.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                initTab(position);
            }
        });
    }

    private void initTab(int type) {
        binding.llMsgView1.setOnClickListener(view -> {
            initTab(0);
            binding.vpMessage.setCurrentItem(0);
        });
        binding.llMsgView2.setOnClickListener(view -> {
            initTab(1);
            binding.vpMessage.setCurrentItem(1);
        });

        if (type == 0) {
            binding.llMsgView1.setTextSize(TypedValue.COMPLEX_UNIT_SP,21);
            binding.llMsgView1.setTypeface(Typeface.DEFAULT_BOLD);
            binding.discRel2.setVisibility(View.VISIBLE);
            binding.llMsgView2.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
            binding.llMsgView2.setTypeface(Typeface.DEFAULT);
            binding.discRel1.setVisibility(View.GONE);
        }
        if (type == 1) {
            binding.llMsgView1.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
            binding.llMsgView1.setTypeface(Typeface.DEFAULT);
            binding.discRel2.setVisibility(View.GONE);
            binding.llMsgView2.setTextSize(TypedValue.COMPLEX_UNIT_SP,21);
            binding.llMsgView2.setTypeface(Typeface.DEFAULT_BOLD);
            binding.discRel1.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
//        TalkingDataSDK.onPageBegin(getActivity().getApplicationContext(), this.toString());
    }

    @Override
    public void onPause() {
        if(!this.isHidden()){
//            TalkingDataSDK.onPageEnd(getActivity().getApplicationContext(), this.toString());
        }
        super.onPause();
    }
}
